import { Application } from 'egg';
import * as pageFindPlugin from 'mongoose-paginate';
import { CleanAppointmentOrderStatus, CleanMemberOrderTypeEnums, CleanAppointmentOrderStatusName } from '../constants';

/**
 * 保洁预约订单集合
 */
export default (app: Application) => {
  const mongoose = app.mongoose;
  const ObjectId = mongoose.Schema.Types.ObjectId;
  const cleanAppointmentOrderSchema = new mongoose.Schema({
    // 门店Id
    siteId: {
      type: ObjectId,
      required: true,
    },
    // 预约产品
    product: {
      type: ObjectId,
      required: true,
      ref: 'Cleanproduct',
    },
    // 订单id
    orderId: {
      type: Number,
      required: true,
    },
    // 订单类型
    orderType: {
      type: String,
      default: CleanMemberOrderTypeEnums.cleanAppointment,
      required: true,
    },
    // 用户Id
    userId: {
      type: ObjectId,
      required: true,
    },
    // 用户姓名
    userName: {
      type: String,
      required: true,
    },
    // 电话号码
    phoneNumber: {
      type: Number,
      required: true,
    },
    // 服务门店
    siteName: {
      type: String,
      required: true,
    },
    // 详情地址
    address: {
      type: String,
      required: true,
    },
    // 状态
    status: {
      type: Number,
      default: CleanAppointmentOrderStatus.unConfirmed,
      enum: CleanAppointmentOrderStatus,
      required: true,
    },
    // 原因描述
    reason: {
      type: String,
      required: false,
    },
  }, { timestamps: true, toJSON: { virtuals: true, getters: true } });

  // 状态描述
  // tslint:disable-next-line:ter-prefer-arrow-callback
  cleanAppointmentOrderSchema.virtual('statusName').get(function (this: any) {
    return CleanAppointmentOrderStatusName[this.status];
  });

  cleanAppointmentOrderSchema.index({ siteId: 1 });
  cleanAppointmentOrderSchema.plugin(pageFindPlugin, {});
  return mongoose.model('Cleanappointmentorder', cleanAppointmentOrderSchema);
};
